﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Common
{
    public class Bill3
    {
        public static Result<string> IndexEdit(Models.MVC.Bill3IndexModel m)
        {
            Result<string> result = new Result<string>();
            try
            {
                if (m.ID == 0)
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillIndex3 where Code='" + m.Code + "' and IsDeleted=0")) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "卡号已经存。";
                        result.Data = "卡号已经存。";
                        return result;
                    }

                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[BillIndex3]
           ([Code]
           ,[GuanKe]
           ,[HanLiao]
           ,[JiaoTi]
           ,[NeiYinXian]
           ,[QiTa]
           ,[Qty]
           ,[State]
           ,[CreateDate]
           ,[JiHuaOrderID]
           ,[IsDeleted])
     VALUES
           (
'" + m.Code + @"',
'" + m.GuanKe + @"',
'" + m.HanLiao + @"',
'" + m.JiaoTi + @"',
'" + m.NeiYinXian + @"',
'" + m.QiTa + @"',
'" + m.Qty + @"',
'0',
'"+m.CreateDate+ @"',
'" + m.JiHuaOrderID + @"',
0
)
");
                    int IndexID = Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select ID from BillIndex3 where Code='" + m.Code + "' and IsDeleted=0"));
                    if (m.MoBanID > 0)
                    {
                        FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
insert into BillProcedures3 (BillIndexID,ProcedureID,Remark,ShowIndex,BiaoZhunQty)
select " + IndexID + @",ProcedureID,GongYi,ShowIndex,BiaoZhunQty
from BillTempletProcedures
where IndexID=" + m.MoBanID + @" and IsDeleted=0
order by ShowIndex

");
                    }
//                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
//declare @BillID int=" + IndexID + @",@GoodsID int=" + m.GoodsID + @"
//insert into BillName2BillBOM (BillID,GoodsID,Type,Qty,LingQty)
//select @BillID,GoodsID,Type,Qty,Qty*" + m.Qty + @"
//from BillName2GoodBOM
//where MainGoodsID=@GoodsID");
                    
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
                else
                {
                    if (Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle("select count(*) from BillIndex3 where Code='" + m.Code + "' and IsDeleted=0 and ID<>" + m.ID)) > 0)
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "卡号已经存。";
                        result.Data = "卡号已经存。";
                        return result;
                    }
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[BillIndex3]
   SET [Code] = '" + m.Code + @"'
      ,[GuanKe] = '" + m.GuanKe + @"'
      ,[Number] = '" + m.Number + @"'
      ,[HanLiao] = '" + m.HanLiao + @"'
      ,[JiaoTi] = '" + m.JiaoTi + @"'
      ,[NeiYinXian] = '" + m.NeiYinXian + @"'
      ,[QiTa] = '" + m.QiTa + @"'
      ,[Qty] = '" + m.Qty + @"'
      ,[CreateDate] = '" + m.CreateDate + @"'
      ,[JiHuaOrderID] = '" + m.JiHuaOrderID + @"'
 WHERE ID=" + m.ID + @";

");
                    result.Type = AjaxResultType.Success;
                    result.Content = "成功";
                    result.Data = "成功";
                }
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = e.Message;
            }
            return result;

        }

        public static Result<int> BillDetailsEdit(Models.MVC.Bill3DetailsModel m)
        {
            Result<int> result = new Result<int>();
            try
            {
                string err = FreeADO.DBUtility.DbHelperSQL.GetSingle("select dbo.Fn_Value_CheckProQty(3," + m.BillProcedureID + "," + m.ID + "," + m.InQty + "," + m.FanQty + "," + m.FailQty + "," + m.OutQty + ")").ToString();
                if (err != "")
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = err;
                    result.Data = 0;
                    return result;
                }
                string err2 = FreeADO.DBUtility.DbHelperSQL.GetSingle("select dbo.Fn_Value_CheckProDate(3," + m.BillProcedureID + "," + m.ID + ",'" + m.BeginDate + "','" + m.EndDate + "')").ToString();
                if (err2 != "")
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = err2;
                    result.Data = 0;
                    return result;
                }

                if (m.ID == 0)
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
INSERT INTO [dbo].[BillDetails3]
           ([BillProcedureID]
           ,[GongYiID]
           ,[InQty]
           ,[OutQty]
           ,[FanQty]
           ,[FailQty]
           ,[FailRemark]
           ,[TaskTime]
           ,[OperatorUserID]
           ,[CreateDate]
           ,[SheBei]
           ,[TiaoJian]
           ,[BeginDate]
           ,[EndDate]
           ,[CheckRemark]
           ,[Remark])
     VALUES
          (
            '" + m.BillProcedureID + @"',
            '" + m.GongYiID + @"',
            '" + m.InQty + @"',
            '" + m.OutQty + @"',
            '" + m.FanQty + @"',
            '" + m.FailQty + @"',
            '" + m.FailRemark + @"',
            '" + m.TaskTime + @"',
            '" + m.OperatorUserID + @"',
            '" + m.CreateDate + @"',
            '" + m.SheBei + @"',
            '" + m.TiaoJian + @"',
            '" + m.BeginDate + @"',
            '" + m.EndDate + @"',
            '" + m.CheckRemark + @"',
            '" + m.Remark + @"'
            )
");
                }
                else
                {
                    FreeADO.DBUtility.DbHelperSQL.ExecuteSql(@"
UPDATE [dbo].[BillDetails3]
   SET [BillProcedureID] = '" + m.BillProcedureID + @"'
      ,[GongYiID] = '" + m.GongYiID + @"'
      ,[InQty] = '" + m.InQty + @"'
      ,[OutQty] = '" + m.OutQty + @"'
      ,[FanQty] = '" + m.FanQty + @"'
      ,[FailQty] = '" + m.FailQty + @"'
      ,[FailRemark] = '" + m.FailRemark + @"'
      ,[TaskTime] = '" + m.TaskTime + @"'
      ,[OperatorUserID] = '" + m.OperatorUserID + @"'
      ,[CreateDate] = '" + m.CreateDate + @"'
      ,[SheBei] = '" + m.SheBei + @"'
      ,[TiaoJian] = '" + m.TiaoJian + @"'
      ,[CheckRemark] = '" + m.CheckRemark + @"'
      ,[Remark] = '" + m.Remark + @"'
      ,[BeginDate] = '" + m.BeginDate + @"'
      ,[EndDate] = '" + m.EndDate + @"'
 WHERE ID=" + m.ID);
                }
                result.Type = AjaxResultType.Success;
                result.Content = "成功。";
                result.Data = 0;
            }
            catch (Exception e)
            {

                result.Type = AjaxResultType.Error;
                result.Content = e.Message;
                result.Data = 0;
            }
            return result;
        }

    }
}